<%@page contentType="text/html" import="java.util.regex.*"
        pageEncoding="UTF-8"%>
<%@page import="javax.jcr.Repository,
        org.apache.jackrabbit.j2ee.RepositoryAccessServlet,
        org.apache.jackrabbit.util.Text,
        javax.jcr.Session,
        javax.jcr.SimpleCredentials,
        javax.jcr.query.Query,
        javax.jcr.query.RowIterator,
        java.text.NumberFormat,
        javax.jcr.query.Row,
        javax.jcr.Node,
        java.net.URLEncoder,
        java.text.SimpleDateFormat,
        java.text.DateFormat,
        java.util.List,
        java.util.ArrayList,
        java.util.Iterator,
        javax.jcr.Value,
        org.apache.jackrabbit.core.version.InternalVersionManager,
        org.apache.jackrabbit.core.version.InternalVersionManagerImpl,
        org.apache.jackrabbit.core.persistence.pool.BundleDbPersistenceManager,
        org.apache.jackrabbit.core.RepositoryImpl,
        javax.jcr.RepositoryException"%>
<%
    {
      //System.out.println("call???");
      String q = request.getParameter("term");
      if (q == null || q.isEmpty()) {
        return;
      }
        q= q.trim();
      // System.out.println("term=" + q);
       String fixed = q;
       int sp = q.lastIndexOf(" ");
       if (sp >0) {
         fixed = q.substring(0,sp).trim();
         q = q.substring(sp+1);
         }
        else fixed = "";

      java.sql.Connection con = org.apache.lucene.search.spell.SpellChecker.getCon();
      java.sql.Statement st = con.createStatement();
      java.sql.ResultSet rs = st.executeQuery("SELECT * FROM Word where word like '" + q + "%'");
      int i=0;
      StringBuilder sb = new StringBuilder();
      //org.apache.
      sb.append("[");
      while (rs.next()) {
        i++;
        if (i>15) break;
        sb.append("\"").append(fixed).append(" ").append(rs.getString(1)).append("\"").append(",");
        //out.print(rs.getString(1));
        //out.println(" ");
      }
      sb.deleteCharAt(sb.length()-1);
      sb.append("]");
      out.println(sb);
      //System.out.println(sb);
      rs.close();
      st.close();
      con.close();
      if (true) {
        return;
      }
    }

    Repository rep;
    Session jcrSession = (Session) session.getAttribute("ajax");
    if (jcrSession == null) {
      try {
        rep = RepositoryAccessServlet.getRepository(pageContext.getServletContext());
        jcrSession = rep.login(new SimpleCredentials("admin", "admin".toCharArray()));
      } catch (Exception e) {
      }
    }
    String q = request.getParameter("q");
    // SPELLCHECK('jackrabit') will always evaluate to true
    Query query = jcrSession.getWorkspace().getQueryManager().createQuery("/jcr:root[rep:spellcheck('" + q + "')]/(rep:spellcheck())", Query.XPATH);

    //.createQuery("SELECT rep:spellcheck() FROM nt:base WHERE jcr:path = '/' AND SPELLCHECK('salt')", Query.SQL);
    RowIterator rows = query.execute().getRows();
    // the above query will always return the root node no matter what string we check
    while (rows.hasNext()) {
      Row r = rows.nextRow();
      // get the result of the spell checking
      Value v = r.getValue("rep:spellcheck()");
      if (v == null) {
        // out.println("no suggestions");
        // no suggestion returned, the spelling is correct or the spell checker
        // does not know how to correct it.
      } else {
        String suggestion = v.getString();
        out.println(suggestion);
      }
    }

    if (jcrSession != null) {
      jcrSession.logout();
    }
%>